{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "author = \"kyubyong. https://github.com/Kyubyong/numpy_exercises\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1.11.3'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q1. Concatenate x1 and x2."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello world' 'Say something']\n"
     ]
    }
   ],
   "source": [
    "x1 = np.array(['Hello', 'Say'], dtype=np.str)\n",
    "x2 = np.array([' world', ' something'], dtype=np.str)\n",
    "out = np.char.add(x1, x2)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q2. Repeat x three time element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello Hello Hello ' 'Say Say Say ']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello ', 'Say '], dtype=np.str)\n",
    "out = np.char.multiply(x, 3)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q3-1. Capitalize the first letter of x element-wise.<br/>\n",
    "Q3-2. Lowercase x element-wise.<br/>\n",
    "Q3-3. Uppercase x element-wise.<br/>\n",
    "Q3-4. Swapcase x element-wise.<br/>\n",
    "Q3-5. Title-case x element-wise.<br/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "capitalized = ['Hello world' 'Say something']\n",
      "lowered = ['hello world' 'say something']\n",
      "uppered = ['HELLO WORLD' 'SAY SOMETHING']\n",
      "swapcased = ['HEllO WOrlD' 'sAY SoMETHING']\n",
      "titlecased = ['Hello World' 'Say Something']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['heLLo woRLd', 'Say sOmething'], dtype=np.str)\n",
    "capitalized = np.char.capitalize(x)\n",
    "lowered = np.char.lower(x)\n",
    "uppered = np.char.upper(x)\n",
    "swapcased = np.char.swapcase(x)\n",
    "titlecased = np.char.title(x)\n",
    "print(\"capitalized =\", capitalized)\n",
    "print(\"lowered =\", lowered)\n",
    "print(\"uppered =\", uppered)\n",
    "print(\"swapcased =\", swapcased)\n",
    "print(\"titlecased =\", titlecased)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q4. Make the length of each element 20 and the string centered / left-justified / right-justified with paddings of `_`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "centered = ['____hello world_____' '___say something____']\n",
      "left = ['hello world_________' 'say something_______']\n",
      "right = ['_________hello world' '_______say something']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['hello world', 'say something'], dtype=np.str)\n",
    "centered = np.char.center(x, 20, fillchar='_')\n",
    "left = np.char.ljust(x, 20, fillchar='_')\n",
    "right = np.char.rjust(x, 20, fillchar='_')\n",
    "\n",
    "print(\"centered =\", centered)\n",
    "print(\"left =\", left)\n",
    "print(\"right =\", right)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q5. Encode x in cp500 and decode it again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "encoded = [b'\\x88\\x85\\x93\\x93\\x96@\\xa6\\x96\\x99\\x93\\x84'\n",
      " b'\\xa2\\x81\\xa8@\\xa2\\x96\\x94\\x85\\xa3\\x88\\x89\\x95\\x87']\n",
      "decoded = ['hello world' 'say something']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['hello world', 'say something'], dtype=np.str)\n",
    "encoded = np.char.encode(x, 'cp500')\n",
    "decoded = np.char.decode(encoded,'cp500')\n",
    "print(\"encoded =\", encoded)\n",
    "print(\"decoded =\", decoded)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q6. Insert a space between characters of x."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['h e l l o   w o r l d' 's a y   s o m e t h i n g']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['hello world', 'say something'], dtype=np.str)\n",
    "out = np.char.join(\" \", x)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q7-1. Remove the leading and trailing whitespaces of x element-wise.<br/>\n",
    "Q7-2. Remove the leading whitespaces of x element-wise.<br/>\n",
    "Q7-3. Remove the trailing whitespaces of x element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stripped = ['hello world' 'say something']\n",
      "lstripped = ['hello world   ' 'say something\\n']\n",
      "rstripped = ['   hello world' '\\tsay something']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['   hello world   ', '\\tsay something\\n'], dtype=np.str)\n",
    "stripped = np.char.strip(x)\n",
    "lstripped = np.char.lstrip(x)\n",
    "rstripped = np.char.rstrip(x)\n",
    "print(\"stripped =\", stripped)\n",
    "print(\"lstripped =\", lstripped)\n",
    "print(\"rstripped =\", rstripped)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q8. Split the element of x with spaces."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['Hello', 'my', 'name', 'is', 'John']]\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello my name is John'], dtype=np.str)\n",
    "out = np.char.split(x)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q9. Split the element of x to multiple lines."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[['Hello', 'my name is John']]\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello\\nmy name is John'], dtype=np.str)\n",
    "out = np.char.splitlines(x)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q10. Make x a numeric string of 4 digits with zeros on its left."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['0034']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['34'], dtype=np.str)\n",
    "out = np.char.zfill(x, 4)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q11. Replace \"John\" with \"Jim\" in x."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Hello nmy name is Jim']\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello nmy name is John'], dtype=np.str)\n",
    "out = np.char.replace(x, \"John\", \"Jim\")\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparison"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q12. Return x1 == x2, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True  True  True False]\n"
     ]
    }
   ],
   "source": [
    "x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)\n",
    "x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)\n",
    "out = np.char.equal(x1, x2)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q13. Return x1 != x2, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False False False False  True]\n"
     ]
    }
   ],
   "source": [
    "x1 = np.array(['Hello', 'my', 'name', 'is', 'John'], dtype=np.str)\n",
    "x2 = np.array(['Hello', 'my', 'name', 'is', 'Jim'], dtype=np.str)\n",
    "out = np.char.not_equal(x1, x2)\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String information"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q14. Count the number of \"l\" in x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 0 0 0 1]\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)\n",
    "out = np.char.count(x, \"l\")\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q15. Count the lowest index of \"l\" in x, element-wise."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2 -1 -1 -1  2]\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello', 'my', 'name', 'is', 'Lily'], dtype=np.str)\n",
    "out = np.char.find(x, \"l\")\n",
    "print(out)\n",
    "\n",
    "# compare\n",
    "# print(np.char.index(x, \"l\"))\n",
    "# => This raises an error!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q16-1. Check if each element of x is composed of digits only.<br/>\n",
    "Q16-2. Check if each element of x is composed of lower case letters only.<br/>\n",
    "Q16-3. Check if each element of x is composed of upper case letters only."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Digits only = [False False False  True False False]\n",
      "Lower cases only = [False False  True False  True  True]\n",
      "Upper cases only = [False  True False False False False]\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['Hello', 'I', 'am', '20', 'years', 'old'], dtype=np.str)\n",
    "out1 = np.char.isdigit(x)\n",
    "out2 = np.char.islower(x)\n",
    "out3 = np.char.isupper(x)\n",
    "print(\"Digits only =\", out1)\n",
    "print(\"Lower cases only =\", out2)\n",
    "print(\"Upper cases only =\", out3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q17. Check if each element of x starts with \"hi\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True  True  True]\n"
     ]
    }
   ],
   "source": [
    "x = np.array(['he', 'his', 'him', 'his'], dtype=np.str)\n",
    "out = np.char.startswith(x, \"hi\")\n",
    "print(out)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
